package com.zy.design.pattern.single;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhaoyi
 * @date 2021/3/29 15:05

 * 单例模式的一些应用场景，
 *  避免资源访问冲突
 *  表示业务概念上的全局唯一类
 */
public class SingleTest {

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executor = Executors.newFixedThreadPool(10);

        //基于静态内部类的
        executor.submit(()->StaticClassSingle.getInstance().sayHello());
        executor.submit(()->StaticClassSingle.getInstance().sayHello());
        executor.submit(()->StaticClassSingle.getInstance().sayHello());
        executor.submit(()->StaticClassSingle.getInstance().sayHello());
        executor.submit(()->StaticClassSingle.getInstance().sayHello());
        TimeUnit.SECONDS.sleep(1);
        System.out.println("------------------------------------");

        //基于枚举的
        executor.submit(EnumSingle.INSTANCE::sayHello);
        executor.submit(EnumSingle.INSTANCE::sayHello);
        executor.submit(EnumSingle.INSTANCE::sayHello);
        executor.submit(EnumSingle.INSTANCE::sayHello);
        executor.submit(EnumSingle.INSTANCE::sayHello);
        TimeUnit.SECONDS.sleep(1);
        System.out.println("------------------------------------");


        //双检索的
        executor.submit(()->DoubleCheckSingle.getInstance().sayHello());
        executor.submit(()->DoubleCheckSingle.getInstance().sayHello());
        executor.submit(()->DoubleCheckSingle.getInstance().sayHello());
        executor.submit(()->DoubleCheckSingle.getInstance().sayHello());
        executor.submit(()->DoubleCheckSingle.getInstance().sayHello());
        TimeUnit.SECONDS.sleep(1);
        System.out.println("------------------------------------");

        //饿汉式 - 最简单的
        executor.submit(()->SimpleSingle.getInstance().sayHello());
        executor.submit(()->SimpleSingle.getInstance().sayHello());
        executor.submit(()->SimpleSingle.getInstance().sayHello());
        executor.submit(()->SimpleSingle.getInstance().sayHello());
        executor.submit(()->SimpleSingle.getInstance().sayHello());






        executor.shutdown();




    }

}
